﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manage_forget_psw : System.Web.UI.Page
{
    public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //
        if (Session["mobile_code"].ToString() == TextBox3.Text.ToString())
        {
            DataClassesDataContext db = new DataClassesDataContext();
            
            string s1 = TextBox2.Text.Substring(5, 6);
            db.update_psw(TextBox2.Text.ToString(),s1);
            Response.Write("<script>alert('密码已初始化为手机号后6位！');location.href='../adminlogin.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('验证码错误，请重新获取！')</script>");
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int?i;
        DataClassesDataContext db=new DataClassesDataContext();
        i = db.find_phone(TextBox2.Text.ToString(), TextBox1.Text.ToString());
        if (i == 1)
        {
            string account = "C59992503";//用户名是登录用户中心->验证码、通知短信->帐户及签名设置->APIID
            string password = "15f292e84dd3ab7ee04431a4b9e477d8"; //密码是请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
            string mobile = TextBox2.Text.ToString();
            Random rad = new Random();
            int mobile_code = rad.Next(1000, 10000);
            string content = "您的验证码是：" + mobile_code + " 。请不要把验证码泄露给其他人。";

            //Session["mobile"] = mobile;
            Session["mobile_code"] = mobile_code;

            string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postData.Length;

            Stream newStream = myRequest.GetRequestStream();
            // Send the data.
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();

            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                Response.Write("<script>alert('发送成功！请尽快使用，验证码于一分钟后失效！')</script>");
            }
            else
            {
                Response.Write("<script>alert('发送失败！')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('当前管理员不存在！')</script>");
        }
        
    }
}